package com.lyswkjxgs.report.controller;

import com.lyswkjxgs.report.domain.AjaxResult;
import com.lyswkjxgs.report.domain.ConfigDTO;
import com.lyswkjxgs.report.service.ConfigService;
import com.lyswkjxgs.report.service.ReportService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class ConfigController {

    @Autowired
    private ConfigService configService;

    @Autowired
    private ReportService reportService;

    @RequestMapping(value = "/api/config/get")
    public AjaxResult<ConfigDTO> getConfig(){
        return AjaxResult.successData(configService.getConfig());
    }

    @RequestMapping(value = "/api/config/test", method = RequestMethod.POST)
    public AjaxResult<String> testConfigConnection(@RequestBody ConfigDTO configDTO){
        String errorInfo = reportService.testConfig(configDTO);
        if (StringUtils.isNotBlank(errorInfo)){
            return AjaxResult.error(errorInfo);
        }
        else {
            return AjaxResult.success();
        }
    }

    @RequestMapping(value = "/api/config/save", method = RequestMethod.POST)
    public AjaxResult<String> save(@RequestBody ConfigDTO configDTO){
        configService.saveConfig(configDTO);
        return AjaxResult.success();
    }




}
